import myAjax from '../static/myAjax.js'

window.onload = function () {
    loadInfo();
    document.getElementById('add').onclick = function () {
        let jobId = document.getElementById('jobId').value;
        let tname = document.getElementById('tname').value;
        let school = document.getElementById('school').value;
        let className = document.getElementById('className').value;
        let password = document.getElementById('password').value;
        if (!jobId || !tname || !school || !password) {
            layer.open({
                title: '信息',
                content: '工号，姓名，学校和密码不可为空！'
            })
            return;
        }
        myAjax('/teacher/addTeacher', 'POST', 'tname=' + tname + '&school=' + school + '&className=' + className
            + '&jobId=' + jobId + '&passwor=' + password)
            .then(
                res => {
                    if (res.code == 200) {
                        layer.open({
                            title: '信息',
                            content: res.msg
                        })
                        document.getElementById('jobId').value = '';
                        document.getElementById('tname').value = '';
                        document.getElementById('school').value = '';
                        document.getElementById('className').value = '';
                        document.getElementById('password').value = '';
                        $('#myModal').modal('toggle');
                        loadInfo();
                    } else {
                        layer.open({
                            title: '信息',
                            content: res.msg
                        })
                    }
                }
            )
    }
    document.getElementById('modify').onclick = function(){
        let jobId = document.getElementById('modifyjobId').value;
        let tname = document.getElementById('modifytname').value;
        let school = document.getElementById('modifyschool').value;
        let className = document.getElementById('modifyclassName').value;
        let password = document.getElementById('modifypassword').value;
        if (!jobId || !tname || !school || !password) {
            layer.open({
                title: '信息',
                content: '工号，姓名，学校和密码不可为空！'
            })
            return;
        }
        myAjax('/teacher/setTeacher', 'POST', 'tname=' + tname + '&school=' + school + '&className=' + className
            + '&jobId=' + jobId + '&passwor=' + password)
            .then(
                res => {
                    if (res.code == 200) {
                        layer.open({
                            title: '信息',
                            content: res.msg
                        })
                        $('#modifyModal').modal('toggle');
                        loadInfo();
                    } else {
                        layer.open({
                            title: '信息',
                            content: res.msg
                        })
                    }
                }
            )
    }
}


function loadInfo() {
    myAjax('/teacher/info?' + Math.random(), 'GET')
        .then(
            res => {
                if (res.code == 200) {
                    console.log(res.teachers);
                    let text = '';
                    for (let teacher of res.teachers) {
                        text += `<tr>
                                <td>${teacher.jobId}</td>
                                <td>${teacher.tname}</td>
                                <td>${teacher.school}</td>
                                <td>${teacher.class_name}</td>
                                <td>
                                    <button type="button" class="btn btn-danger remove" data-jobId="${teacher.jobId}" >删除</button>
                                    <button type="button" class="btn btn-primary modify" data-teacher='`+JSON.stringify(teacher)+`' data-toggle="modal" data-target="#modifyModal">修改</button>
                                </td>
                            </tr>`;
                    }
                    document.getElementById('teacherInfo').innerHTML = text;
                    document.querySelectorAll('.remove').forEach(
                        (remove,index)=>{
                            remove.onclick = function () {
                                var msg = "您真的确定要删除吗？\n\n请确认！";
                                if (confirm(msg) == true) {
                                    let jobId = this.getAttribute('data-jobId');
                                    myAjax('/teacher/removeTeacher', 'POST', 'jobId=' + jobId)
                                        .then(
                                            res => {
                                                if (res.code == 200) {
                                                    layer.open({
                                                        title: '信息',
                                                        content: res.msg
                                                    })
                                                    loadInfo();
                                                }
                                            }
                                        )
                                } else {
                                    return false;
                                }
                            }
                        }
                    )
                    document.querySelectorAll('.modify').forEach(
                        (remove,index)=>{
                            remove.onclick = function () {
                                let teacher =JSON.parse(this.getAttribute('data-teacher'));
                                console.log(teacher);
                                document.getElementById('modifyjobId').value = teacher.jobId;
                                document.getElementById('modifytname').value = teacher.tname;
                                document.getElementById('modifyschool').value = teacher.school;
                                document.getElementById('modifyclassName').value = teacher.class_name;
                                document.getElementById('modifypassword').value = teacher.password;
                            }
                        }
                    )
                }
            }
        )
}